package com.ioudi.digu.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ioudi.digu.dao.ReceivedDAO;
import com.ioudi.digu.revise.IMessageRevisor;
import com.ioudi.digu.service.IReceivedService;
import com.ioudi.digu.service.UserAwareService;
import com.ioudi.digu.service.support.ListResult;
import com.ioudi.digu.util.IDUtil;
import com.ioudi.digu.vo.ProducedMessage;
import com.ioudi.digu.vo.ReceivedMessage;
import com.ioudi.digu.vo.adapter.ReceivedMessageAdapter;

public class ReceivedService extends UserAwareService implements IReceivedService
{

	private ReceivedDAO receivedDAO;
	
	private IMessageRevisor messageRevisor;
	
	public IMessageRevisor getMessageRevisor()
	{
		return messageRevisor;
	}

	public void setMessageRevisor(IMessageRevisor messageRevisor)
	{
		this.messageRevisor = messageRevisor;
	}

	public ReceivedDAO getReceivedDAO()
	{
		return receivedDAO;
	}

	public void setReceivedDAO(ReceivedDAO receivedDAO)
	{
		this.receivedDAO = receivedDAO;
	}

	public Object createItem(Object item)
	{
		return new Exception("you cant't create received message");
	}
	
	public ReceivedMessage saveReceived(String user, byte topic, ProducedMessage message)
	{
		ReceivedMessage rm = new ReceivedMessage();	
		rm.setUser(user);
		rm.setId(IDUtil.bindID(user, message.getMessageId()));
		rm.setMessage(message);
		if(topic > 0)
		{
			rm.setTopic(topic);
		}
		Object result =  receivedDAO.saveObject(rm);
        return result != null ? (ReceivedMessage)result : null;
	}

	public boolean deleteItem(String[] ids)
	{
		String user = getUser();
		if(user == null)
		{
			return false;
		}
		return receivedDAO.deleteObjects(ids);
	}

	//TODO find the received id from all database
	public Object getItem(String id)
	{
		Object raw = receivedDAO.getObject(id);
		if(raw == null)
		{
			return null;
		}
		return  messageRevisor.outboudRevise(((ReceivedMessage)raw).getMessage());
	}

	/**
	 * You only can get your self received message
	 */
	public List getItems(int index, int maxCount, Map condition)
	{
		return receivedDAO.findObjects(index, maxCount, condition);
	}
	
	/**
	 * You only can get your swlf received message
	 */
	public ListResult getItemsWithTotal(int index, int maxCount, Map condition)
	{
		String user = getUser();
		if(user == null)
		{
			return null;
		}
		if(condition == null)
		{
			condition = new HashMap();	
		}
		condition.put("user", user);
		ListResult result = receivedDAO.findObjectsWithTotal(index, maxCount, condition);
		List contents = result != null ? result.getContents() : null;
		if(contents != null && contents.size() > 0)
		{
			 int i = contents.size() - 1;
			 ProducedMessage pm;
			 while(i >= 0)
			 {
				 pm = ((ReceivedMessage)contents.get(i)).getMessage();
				 contents.set(i, pm);
				 i--;
			 }
			 contents = messageRevisor.outboudRevise(contents);
		}
		return result;
	}
	
	/**
	 * get the location 
	 */
	public List getMessageLocation(int index, int maxCount, Map condition)
	{
		String user = getUser();
		if(user == null)
		{
			return null;
		}
		if(condition == null)
		{
			condition = new HashMap();	
		}
		condition.put("user", user);
		return receivedDAO.findMessageLocation(index, maxCount, condition);
	}

	public Object updateItem(String id, Map updateMap)
	{
		return new Exception("you cant't update a received message");
	}

	

}
